﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using edu.Model;
using System.Data.Common;
using System.Data.SqlClient;
using Discuz.Data;
using System.Data;

namespace edu.Service
{
    public class HomeworkService
    {
        public static int Add(Homework entity)
        {
            string sql = @"insert into edu_homework([Code],[Name],[Description],[TeacherId],[Year],[Month],[Day]) 
                        values(@Code,@Name,@Description,@TeacherId,@Year,@Month,@Day)
                        SELECT SCOPE_IDENTITY()";
            DbParameter[] param ={
                new SqlParameter("Code", entity.Code),
                new SqlParameter("Name",entity.Name),
                new SqlParameter("Description",entity.Description),
                new SqlParameter("TeacherId", entity.TeacherId),
                new SqlParameter("Year",entity.Year),
                new SqlParameter("Month",entity.Month),
                new SqlParameter("Day",entity.Day)
                                };
            object obj = DbHelper.ExecuteScalar(CommandType.Text, sql, param);


            return Convert.ToInt32(obj);
        }

        public static void Check(int teacherId, int year, int month, int day)
        {
            string sql = @"select 1 from edu_homework where teacherId = @teacherId
                            and year=@year and month=@month and day=@day";
            DbParameter[] paras ={ new SqlParameter("teacherId", teacherId),
                                    new SqlParameter("year", year),
                                    new SqlParameter("month", month),
                                    new SqlParameter("day", day)};
            DataTable table = DbHelper.ExecuteDataset(CommandType.Text, sql, paras).Tables[0];

            if (table.Rows.Count == 0)
            {
                Homework work = new Homework();
                work.TeacherId = teacherId;
                work.Year = year;
                work.Month = month;
                work.Day = day;
                work.Description = "系统自动生成";
                Add(work);
            }

        }
    }
}
